﻿using System;
#if NETFX_CORE
using Windows.UI.Xaml;
#else
using System.Windows;
#endif

namespace DaveSexton.Labs.Tests.Category1.UI
{
	public partial class FileDialogsLab : BaseLab
	{
		public FileDialogsLab()
		{
			InitializeComponent();
		}

#if NETFX_CORE
		private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
#else
		private void OpenFileButton_Click(object sender, RoutedEventArgs e)
#endif
		{
			try
			{
#if NETFX_CORE
				var file = await UserInputFileToOpenAsync(title: "Open Test");
#else
				var file = UserInputFileToOpen(title: "Open Test");
#endif

				if (file == null)
				{
					TraceLine("Cancelled.");
				}
				else
				{
					try
					{
						TraceLine("Selected file: {0}", file.FullName);
					}
					catch (System.Security.SecurityException)
					{
						// SecurityException is thrown by Silverlight apps running in a web browser.
						TraceLine("Selected file: {0}", file.Name);
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}

#if NETFX_CORE
		private async void OpenMultipleFilesButton_Click(object sender, RoutedEventArgs e)
#else
		private void OpenMultipleFilesButton_Click(object sender, RoutedEventArgs e)
#endif
		{
			try
			{
#if NETFX_CORE
				var files = await UserInputFilesToOpenAsync(title: "Open Multiple Test");
#else
				var files = UserInputFilesToOpen(title: "Open Multiple Test");
#endif

				if (files.Count == 0)
				{
					TraceLine("Cancelled.");
				}
				else
				{
					foreach (var file in files)
					{
						try
						{
							TraceLine("Selected file: {0}", file.FullName);
						}
						catch (System.Security.SecurityException)
						{
							// SecurityException is thrown by Silverlight apps running in a web browser.
							TraceLine("Selected file: {0}", file.Name);
						}
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}

#if NETFX_CORE
		private async void OpenFolderButton_Click(object sender, RoutedEventArgs e)
#else
		private void OpenFolderButton_Click(object sender, RoutedEventArgs e)
#endif
		{
#if SILVERLIGHT
			TraceLine("A folder selection dialog is not supported on Silverlight.");
			return;
#else
			try
			{
#if NETFX_CORE
				var folder = await UserInputFolderAsync(title: "Open Folder Test");
#else
				var folder = UserInputFolder(title: "Open Folder Test");
#endif

				if (folder == null)
				{
					TraceLine("Cancelled.");
				}
				else
				{
					TraceLine("Selected folder: {0}", folder);
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
#endif
		}

#if NETFX_CORE
		private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
#else
		private void SaveFileButton_Click(object sender, RoutedEventArgs e)
#endif
		{
			try
			{
#if NETFX_CORE
				using (var stream = await UserInputFileToSaveAsync(title: "Save Test"))
#else
				using (var stream = UserInputFileToSave(title: "Save Test"))
#endif
				{
					if (stream == null)
					{
						TraceLine("Cancelled.");
					}
					else
					{
						try
						{
							TraceLine("Selected file: {0}", stream.Name);
						}
						catch (System.Security.SecurityException)
						{
							// SecurityException is thrown by Silverlight apps running in a web browser.
							TraceLine("Selected file: [Name Unknown]");
						}
					}
				}
			}
			catch (NotSupportedException ex)
			{
				TraceError(ex.Message);
			}
		}
	}
}